﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using HCI.Questionaire.Entities;

namespace HCI.Questionaire.QuestionRender
{
    public partial class QuestionGroup1Render : UserControl, IQuestionGroupRenderControl
    {
        private int _OtherAnswerTextBoxHeight = 30;
        private StackPanel _currentFieldItem;

        public QuestionGroup1Render()
        {
            InitializeComponent();
        }

        private void StackPanel_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this.DataContext is MultipleAnswerQuestion)
            {
                var selectAnswer = ((StackPanel)sender).DataContext as Answer;
                selectAnswer.IsAnswered = !selectAnswer.IsAnswered;

                if (selectAnswer != null)
                {
                    if (selectAnswer is ITextAnswer)
                    {
                        if (selectAnswer.IsAnswered)
                        {
                            var textBlock = (TextBlock)((StackPanel)((Grid)((Border)((StackPanel)sender).Children[0]).Child).Children[0]).Children[0];
                            //textBlock.Visibility = Visibility.Collapsed;
                            textBlock.Text = string.Format("อื่นๆ ");

                            ((Border)((StackPanel)sender).Children[0]).Height = 55;
                            var textBox = (TextBox)((StackPanel)((Grid)((Border)((StackPanel)sender).Children[0]).Child).Children[0]).Children[1];
                            textBox.Height = _OtherAnswerTextBoxHeight;
                            textBox.Visibility = Visibility.Visible;
                            this.Dispatcher.BeginInvoke(delegate { textBox.Focus(); });
                        }
                        else
                        {
                            var textBlock = (TextBlock)((StackPanel)((Grid)((Border)((StackPanel)sender).Children[0]).Child).Children[0]).Children[0];
                            //textBlock.Visibility = Visibility.Collapsed;
                            textBlock.Text = string.Format("อื่นๆ (โปรดระบุ).............");

                            ((Border)((StackPanel)sender).Children[0]).Height = 55;
                            var textBox = (TextBox)((StackPanel)((Grid)((Border)((StackPanel)sender).Children[0]).Child).Children[0]).Children[1];
                            textBox.Height = _OtherAnswerTextBoxHeight;
                            textBox.Visibility = Visibility.Collapsed;
                        }
                    }
                }
            }
            else
            {
                // clear all selected in current question
                var currentQuestion = this.DataContext as Question;
                if (currentQuestion != null)
                {
                    foreach (var item in currentQuestion.AnswerCollection)
                    {
                        item.IsAnswered = false;
                    }
                }

                if (this._currentFieldItem != null)
                {
                    ((Border)this._currentFieldItem.Children[0]).BorderBrush = new SolidColorBrush(Color.FromArgb(255, 204, 204, 204));
                    ((Border)this._currentFieldItem.Children[0]).BorderThickness = new Thickness(1.0);

                    ((Answer)this._currentFieldItem.DataContext).IsAnswered = false;

                    if (((Answer)this._currentFieldItem.DataContext) is ITextAnswer)
                    {
                        var oldAnswer = ((Answer)this._currentFieldItem.DataContext);
                        var textBox = (TextBox)((StackPanel)((Grid)((Border)this._currentFieldItem.Children[0]).Child).Children[0]).Children[1];
                        textBox.Visibility = Visibility.Collapsed;

                        var textBlock = (TextBlock)((StackPanel)((Grid)((Border)this._currentFieldItem.Children[0]).Child).Children[0]).Children[0];
                        textBlock.Visibility = Visibility.Visible;

                        ((Border)this._currentFieldItem.Children[0]).Height = 55;

                        if (oldAnswer.AnswerResult != null)
                        {
                            //textBlock.Text = string.Format("อื่นๆ (โปรดระบุ).............");
                        }
                    }
                    //children1.Text = children.Text;
                    //selectAnswer.AnswerResult = children1.Text;
                    //children.Focus();
                }

                ((Border)((StackPanel)sender).Children[0]).BorderBrush = this.fieldsBorderBrush;
                ((Border)((StackPanel)sender).Children[0]).BorderThickness = new Thickness(1.5);
                this._currentFieldItem = (StackPanel)sender;

                // Reder Checked image here
                // How to set selected to the answer and bind data again?
                var selectAnswer = ((StackPanel)sender).DataContext as Answer;
                if (selectAnswer != null)
                {
                    selectAnswer.IsAnswered = !selectAnswer.IsAnswered;

                    if (selectAnswer is ITextAnswer)
                    {
                        var textBlock = (TextBlock)((StackPanel)((Grid)((Border)this._currentFieldItem.Children[0]).Child).Children[0]).Children[0];
                        //textBlock.Visibility = Visibility.Collapsed;
                        textBlock.Text = string.Format("อื่นๆ ");

                        ((Border)this._currentFieldItem.Children[0]).Height = 55;
                        var textBox = (TextBox)((StackPanel)((Grid)((Border)this._currentFieldItem.Children[0]).Child).Children[0]).Children[1];
                        textBox.Height = _OtherAnswerTextBoxHeight;
                        textBox.Visibility = Visibility.Visible;
                        ((StackPanel)sender).UpdateLayout();
                        
                        //textBox.Focus();
                        this.Dispatcher.BeginInvoke(delegate { textBox.Focus(); });
                    }
                }
            }
            
        }

        private void _field_MouseEnter(object sender, MouseEventArgs e)
        {
            ((Border)sender).BorderBrush = this.fieldsBorderBrush;
            ((Border)sender).BorderThickness = new Thickness(1.5);

            this.fieldMouseOver.Stop();
            Storyboard.SetTarget(this.fieldMouseOver, (DependencyObject)sender);
            this.fieldMouseOver.Begin();
        }

        private void _field_MouseLeave(object sender, MouseEventArgs e)
        {
            if (this._currentFieldItem != null && ((Border)((StackPanel)this._currentFieldItem).Children[0]) == ((Border)sender))
            {
                return;
            }

            ((Border)sender).BorderBrush = new SolidColorBrush(Colors.Gray);
            ((Border)sender).BorderThickness = new Thickness(1.5);
            this.fieldMouseOut.Stop();
            Storyboard.SetTarget(this.fieldMouseOut, (DependencyObject)sender);
            this.fieldMouseOut.Begin();

        }

        #region IQuestionGroupRenderControl Members

        public void InitailRender(HCI.Questionaire.QuestionGroup.IQuestionGroup questionGroup)
        {
            if (questionGroup.CurrentQuestionIndex < 0)
                questionGroup.NextQuestion();

            this._currentFieldItem = null;
            // List of Question object
            var ques = questionGroup.QuestionList[questionGroup.CurrentQuestionIndex];
            foreach (var item in ques.AnswerCollection)
            {
                item.AnswerIndex = new Duration(new System.TimeSpan(0, 0, 0, 1));
            }
            this.DataContext = ques;
        }

        public string QuestionRunningNumber
        {
            get
            {
                return this.txtQuestionRunningNumber.Text;
            }
            set
            {
                this.txtQuestionRunningNumber.Text = value;
            }
        }
        #endregion

        private void _answerList_LayoutUpdated(object sender, System.EventArgs e)
        {
            var items = _answerList.Items;
        }

        private void TextBox_KeyUp(object sender, KeyEventArgs e)
        {
            var contanerBinder = (StackPanel)((TextBox)sender).Parent;
            if (e.Key == Key.Enter)
            {
                if (contanerBinder != null && ((Answer)contanerBinder.DataContext) is ITextAnswer)
                {
                    var oldAnswer = ((Answer)contanerBinder.DataContext);
                    var textBox = (TextBox)contanerBinder.Children[1];
                    textBox.Visibility = Visibility.Collapsed;

                    var textBlock = (TextBlock)contanerBinder.Children[0];
                    textBlock.Visibility = Visibility.Visible;
                    oldAnswer.IsAnswered = true;

                    ((Border)((Grid)((StackPanel)((TextBox)sender).Parent).Parent).Parent).Height = textBlock.Height;
                    textBlock.Text = string.Format("อื่นๆ   {0}", textBox.Text);
                    oldAnswer.AnswerText = textBlock.Text;
                    textBox.UpdateLayout();
                }
            }
        }
    }
}
